<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class datagrid extends CI_Controller {

    public function __construct()
    {
       parent::__construct();

       // Load required models
       $this->load->model('fs_databases');
       $this->load->model('fs_tables');
    }


	public function index()
	{
        
	}

    public function overview($table)
    {
        $table = $this->db->escape_str($table);
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 25;
        $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';
        $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
        $offset = ($page-1)*$rows;

        // Fix for selecting database
        $dbtmp = $this->fs_tables->select_single_db();

        $result = array();

        // Collect total records for math
        $rs = $dbtmp->query('SELECT * FROM ' .$table);
        $result["total"] = $rs->num_rows();

        // Query to fill the grid
        $rs = $dbtmp->query("SELECT * FROM ".$table." ORDER BY ? ? LIMIT ?,?", array($sort, $order, $offset, $rows));

        $items = array();
        foreach ($rs->result() as $row)
        {
            array_push($items, $row);  
        }
        $result["rows"] = $items;

        echo json_encode($result);
    }

    public function js($width, $height)
    {
        // Set interface width and height (got this from JS)
        // For the easyUI datagrid
        $this->session->set_userdata(array("interface_width" => $width, "interface_height" => $height));
    }

}

/* End of file datagrid.php */
/* Location: ./application/controllers/datagrid.php */